﻿#include "IcePillar.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"
#include "../../Settings.h"
#include "../../../Shared/Functions/Functions.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool IcePillar::getCanMove() const
    {
        return false;
    }

    bool IcePillar::getCanRegen() const
    {
        return false;
    }

    IcePillar::IcePillar(MonsterInfo *info) : MonsterObject(info)
    {
        setDirection(MirDirection::Up);
    }

    void IcePillar::FindTarget()
    {
    }

    void IcePillar::Turn(MirDirection dir)
    {
    }

    bool IcePillar::Walk(MirDirection dir)
    {
        return false;
    }

    void IcePillar::ProcessRegen()
    {
    }

    void IcePillar::ProcessSearch()
    {
    }

    void IcePillar::ProcessRoam()
    {
    }

    int IcePillar::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        ShockTime = 0;

        if (attacker->Info->AI == 6)
        {
            EXPOwner = nullptr;
        }
        else if (attacker->Master != nullptr)
        {
            if (EXPOwner == nullptr || EXPOwner->Dead)
            {
                EXPOwner = attacker->Master;
            }

            if (EXPOwner == attacker->Master)
            {
                EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
            }
        }

        if (getEnvir()->Random->Next(3) == 0)
        {
            CloseAttack(damage);
        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;

    }

    int IcePillar::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        if (damageWeapon)
        {
            attacker->DamageWeapon();
        }

        ShockTime = 0;

        if (Master != nullptr && Master != attacker)
        {
            if (getEnvir()->getTime() > Master->BrownTime && Master->getPKPoints() < 200)
            {
                attacker->BrownTime = getEnvir()->getTime() + Settings::Minute;
            }
        }

        if (EXPOwner == nullptr || EXPOwner->Dead)
        {
            EXPOwner = attacker;
        }

        if (EXPOwner == attacker)
        {
            EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
        }

        if (getEnvir()->Random->Next(3) == 0)
        {
            CloseAttack(damage);
        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        attacker->GatherElement();
        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;
    }

    int IcePillar::Struck(int damage, DefenceType type)
    {
        return 0;
    }

    void IcePillar::ApplyPoison(Poison *p, MapObject *Caster, bool NoResist, bool ignoreDefence)
    {
    }

    void IcePillar::CloseAttack(int damage)
    {
        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation());

        if (targets.empty())
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            S::ObjectEffect *tempVar2 = new S::ObjectEffect();
            tempVar2->ObjectID = targets[i]->ObjectID;
            tempVar2->Effect = SpellEffect::IcePillar;
            Broadcast(tempVar2);

            if (targets[i]->Attacked(this, damage, DefenceType::MACAgility) <= 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                continue;
            }

            PoisonTarget(targets[i], 5, GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]), PoisonType::Frozen, 1000);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void IcePillar::Die()
    {
        S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

        std::vector<MapObject*> targets = FindAllTargets(7, getCurrentLocation(), false);

        if (targets.empty())
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            int delay = Functions::MaxDistance(getCurrentLocation(), targets[i]->getCurrentLocation()) * 50 + 500; //50 MS per Step

            DelayedAction *action = new DelayedAction(DelayedType::Die, getEnvir()->getTime() + delay, {targets[i], damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
        }

        MonsterObject::Die();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void IcePillar::CompleteDeath(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        target->Attacked(this, damage, defence);

        PoisonTarget(target, 5, 5, PoisonType::Frozen, 1000);
    }
}
